﻿using BSChuangXu.API.Read.Appicant.Commend.RoleCommend;
using BSChuangXu.Domain.User;
using BSChuangXu.ErrorCode;
using BSChuangXu.Infrastructure.Interface;
using MediatR;
using System.Data;

namespace BSChuangXu.API.Read.Appicant.CommendHandel.RoleCommandHandler
{
    public class GetRoleCommandHandler : IRequestHandler<GetRoleCommand, APIResult<List<RoleModel>>>
    {
        private readonly IBaseRepository<RoleModel> roleRepos;

        public GetRoleCommandHandler(IBaseRepository<RoleModel> roleRepos)
        {
            this.roleRepos = roleRepos;
        }
        /// <summary>
        /// 角色显示
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task<APIResult<List<RoleModel>>> Handle(GetRoleCommand request, CancellationToken cancellationToken)
        {
            APIResult<List<RoleModel>> result = new APIResult<List<RoleModel>>();
            var list = roleRepos.GetAll().Result.Where(x => x.IsDel == false);
            if (list == null)
            {
                result.Code = APIEnum.查询失败;
                result.Msg = APIEnum.查询失败.ToString();
            }
            else
            {
                result.Code = APIEnum.查询成功;
                result.Msg = APIEnum.查询成功.ToString();
                result.data = list.ToList();
            }
            return Task.FromResult(result);
        }
    }
}
